import { useModel } from "umi";
import { useReactive } from "ahooks";
import { useEffect } from "react";

const useCustomers = () => {
    const { remoteData } = useModel("global");
    const { organizationUuid } = useModel("organization.info");

    const state = useReactive({
        dataLoading: false,
        submitLoading: false,
        customers: [],
        totalCount: 0,
        selectedCustomer: {},
        waitExamineApply: [],
        authorizeRoles: [],
        authorizeRecords: [],
    });

    const loadCustomers = async ({ order, where, oacStat, page, pageSize }) => {
        state.dataLoading = true;
        let params = {
            spUuid: organizationUuid,
        };
        if (where) params.where = where;
        if (oacStat !== undefined) params.oacStat = oacStat;
        if (page && pageSize) {
            params.limit = pageSize;
            params.offset = (page - 1) * pageSize || 0;
        }
        let res = await remoteData({
            object: "bnt-application-operation-center-service",
            serviceName: "/servicer/organization/getCustomers",
            params,
        });
        let customers = res?.customers || [];
        state.totalCount = res?.totalCount || 0;

        if (order) {
            let [key, orderBy] = order;
            customers = helper.sortByKey(customers, key, orderBy);
        }

        state.customers = customers;

        state.dataLoading = false;
    };

    useEffect(() => {
        if (state.customers.length > 0) {
            state.selectedCustomer = state.customers.find((item) => item.oiUuid === state.selectedCustomer.oiUuid);
            !state.selectedCustomer && (state.selectedCustomer = state.customers[0]);
        }
    }, [state.customers]);

    const setSelectedCustomer = (data) => (state.selectedCustomer = data);

    const authorizeApply = async (values, callback) => {
        let res = await remoteData({
            object: "bnt-application-operation-center-service",
            serviceName: "/servicer/organization/customer/authorizeApply",
            params: values,
        });
        if (res && callback) callback(res);
    };

    const loadWaitExamineApply = async () => {
        state.dataLoading = true;
        let res = await remoteData({
            object: "bnt-application-operation-center-service",
            serviceName: "/servicer/organization/customer/getWaitExamineApply",
            params: { spUuid: window.organizationUuid, order: [["oaaaTime", "ASC"]] },
        });
        state.waitExamineApply = res || [];
        state.dataLoading = false;
        return res || [];
    };

    const deleteWaitExamineApply = async (oaaaUuid, callback) => {
        state.dataLoading = true;
        let res = await remoteData({
            object: "bnt-application-operation-center-service",
            serviceName: "/servicer/organization/customer/deleteApply",
            params: { oaaaUuid },
        });
        res && callback && callback(res);
        state.dataLoading = false;
    };

    const loadAuthorizeRoles = async (oiUuid) => {
        state.dataLoading = true;
        let res = await remoteData({
            object: "bnt-application-operation-center-service",
            serviceName: "/servicer/organization/customer/getAuthedRoles",
            params: { oiUuid, order: [["oaarRole", "DESC"]] },
        });
        state.authorizeRoles = res || [];
        state.dataLoading = false;
        return res || [];
    };

    const loadAuthorizeRecords = async (oiUuid) => {
        state.dataLoading = true;
        let res = await remoteData({
            object: "bnt-application-operation-center-service",
            serviceName: "/servicer/organization/customer/getAuthedRecords",
            params: { oiUuid, oaaaExst: "已审核", order: [["oaaaTime", "ASC"]] },
        });
        state.authorizeRecords = res || [];
        state.dataLoading = false;
        return res || [];
    };

    return { ...state, loadCustomers, setSelectedCustomer, authorizeApply, loadWaitExamineApply, deleteWaitExamineApply, loadAuthorizeRoles, loadAuthorizeRecords };
};

export default useCustomers;
